package com.sg.service.biz.framework.impl;


import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.framework.req.BatchQueryPostMatchingStaffingReq;
import com.sg.dto.base.framework.req.QueryPositionListReq;
import com.sg.dto.base.framework.req.QueryStaffingListReq;
import com.sg.dto.biz.framework.req.QueryStaffingStatisticsListReqDto;
import com.sg.dto.biz.framework.res.QueryStaffingStatisticsListRespDto;
import com.sg.entity.Staffing;
import com.sg.service.base.framework.MPositionService;
import com.sg.service.base.framework.MStaffingService;
import com.sg.common.exception.Assert;
import com.wicket.okrframework.common.exception.BizException;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class StatisticalDataServiceImpl
        implements com.wicket.okrframework.biz.service.StatisticalDataService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MPositionService mPositionService;
    @Resource
    private MStaffingService mStaffingService;
    @Resource
    private NbStatisticalData nbStatisticalData;
//@Resource
    //private MSpecMethordTableService mSpecMethordTableService;

    /**
     * D1-3查询编制统计（边界）[1734]
     * gen by moon at 10/2/2022, 12:24:06 AM
     */
    @Trace(operationName = "D1-3查询编制统计（边界）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryPostStaffingStatisticsListRespDto queryPostStaffingStatisticsList(QueryPostStaffingStatisticsListReqDto reqDto) {


        QueryStaffingStatisticsListRespDto getDataCountRes_1 = null;
        if ((reqDto != null && reqDto.getStationId() != null && reqDto != null && reqDto.getPositionGradeId() == null && reqDto != null && reqDto.getDeptId() == null && reqDto != null && reqDto.getPositionId() == null && reqDto != null && reqDto.getOrganizationId() == null) || (reqDto != null && reqDto.getStationId() == null && reqDto != null && reqDto.getPositionGradeId() != null && reqDto != null && reqDto.getDeptId() == null && reqDto != null && reqDto.getPositionId() == null && reqDto != null && reqDto.getOrganizationId() == null) || (reqDto != null && reqDto.getStationId() != null && reqDto != null && reqDto.getPositionGradeId() == null && reqDto != null && reqDto.getDeptId() == null && reqDto != null && reqDto.getPositionId() == null && reqDto != null && reqDto.getOrganizationId() != null) || (reqDto != null && reqDto.getStationId() == null && reqDto != null && reqDto.getPositionGradeId() != null && reqDto != null && reqDto.getDeptId() == null && reqDto != null && reqDto.getPositionId() == null && reqDto != null && reqDto.getOrganizationId() != null) || (reqDto != null && reqDto.getStationId() != null && reqDto != null && reqDto.getPositionGradeId() == null && reqDto != null && reqDto.getDeptId() != null && reqDto != null && reqDto.getPositionId() == null && reqDto != null && reqDto.getOrganizationId() == null)) {
            //if((D1-3查询编制统计（边界）.关联岗位ID 值不等于空  and D1-3查询编制统计（边界）.职务级别ID 值等于空  and D1-3查询编制统计（边界）.部门ID 值等于空  and D1-3查询编制统计（边界）.职务ID 值等于空  and D1-3查询编制统计（边界）.组织ID 值等于空 ) or (D1-3查询编制统计（边界）.关联岗位ID 值等于空  and D1-3查询编制统计（边界）.职务级别ID 值不等于空  and D1-3查询编制统计（边界）.部门ID 值等于空  and D1-3查询编制统计（边界）.职务ID 值等于空  and D1-3查询编制统计（边界）.组织ID 值等于空 ) or (D1-3查询编制统计（边界）.关联岗位ID 值不等于空  and D1-3查询编制统计（边界）.职务级别ID 值等于空  and D1-3查询编制统计（边界）.部门ID 值等于空  and D1-3查询编制统计（边界）.职务ID 值等于空  and D1-3查询编制统计（边界）.组织ID 值不等于空 ) or (D1-3查询编制统计（边界）.关联岗位ID 值等于空  and D1-3查询编制统计（边界）.职务级别ID 值不等于空  and D1-3查询编制统计（边界）.部门ID 值等于空  and D1-3查询编制统计（边界）.职务ID 值等于空  and D1-3查询编制统计（边界）.组织ID 值不等于空 ) or (D1-3查询编制统计（边界）.关联岗位ID 值不等于空  and D1-3查询编制统计（边界）.职务级别ID 值等于空  and D1-3查询编制统计（边界）.部门ID 值不等于空  and D1-3查询编制统计（边界）.职务ID 值等于空  and D1-3查询编制统计（边界）.组织ID 值等于空 ))  1918

            List<Position> listPosition = new ArrayList<>();
            QueryPositionListReq queryPositionListReq = new QueryPositionListReq();
            queryPositionListReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:52392_1_1919
            queryPositionListReq.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:52393_1_1919
            if (reqDto != null) {
                queryPositionListReq.setStationId(reqDto.getStationId());//SimpleFieldAssign//sourceId:52391_1_1919
                queryPositionListReq.setPositionGradeId(reqDto.getPositionGradeId());//SimpleFieldAssign//sourceId:52404_1_1919
            }

            /*1-3-05查询职务列表[147]   */
            Assert.isNull(queryPositionListReq.getSubjectLifeCycle(), "D1-3查询编制统计（边界）-1-3-05查询职务列表-主体生命周期不能为空", false);
            Assert.isNull(queryPositionListReq.getSpaceId(), "D1-3查询编制统计（边界）-1-3-05查询职务列表-创建于空间ID不能为空", false);
            listPosition = mPositionService.queryPositionList(queryPositionListReq);


            List<Staffing> listStaffing = new ArrayList<>();
            if (listPosition != null && listPosition.size() > 0) {
                BatchQueryPostMatchingStaffingReq batchQueryPostMatchingStaffingReq = new BatchQueryPostMatchingStaffingReq();
                batchQueryPostMatchingStaffingReq.setStaffingList(listPosition.stream().map(item -> item.getPositionId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:52300_1_1920
                batchQueryPostMatchingStaffingReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:52301_1_1920
                if (reqDto != null) {
                    batchQueryPostMatchingStaffingReq.setOrganizationId(reqDto.getOrganizationId());//SimpleFieldAssign//sourceId:52493_1_1920
                    batchQueryPostMatchingStaffingReq.setDeptId(reqDto.getDeptId());//SimpleFieldAssign//sourceId:52494_1_1920
                }

                /*1-3-14批量查询编制列表（总编制数）[1282]   */
                Assert.isNull(batchQueryPostMatchingStaffingReq.getSubjectLifeCycle(), "D1-3查询编制统计（边界）-1-3-14批量查询编制列表（总编制数）-主体生命周期不能为空", false);
                listStaffing = mStaffingService.batchQueryPostMatchingStaffing(batchQueryPostMatchingStaffingReq);


            }
            List<Staffing> listStaffing_2 = new ArrayList<>();
            if (listPosition != null && listPosition.size() > 0) {
                BatchQueryPostMatchingStaffingReq batchQueryPostMatchingStaffingReq_1 = new BatchQueryPostMatchingStaffingReq();
                batchQueryPostMatchingStaffingReq_1.setStaffingList(listPosition.stream().map(item -> item.getPositionId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:52303_1_1921
                batchQueryPostMatchingStaffingReq_1.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:52304_1_1921
                batchQueryPostMatchingStaffingReq_1.setInductionState("TRUE");//sourceId:52306_1_1921
                if (reqDto != null) {
                    batchQueryPostMatchingStaffingReq_1.setOrganizationId(reqDto.getOrganizationId());//SimpleFieldAssign//sourceId:52496_1_1921
                    batchQueryPostMatchingStaffingReq_1.setDeptId(reqDto.getDeptId());//SimpleFieldAssign//sourceId:52497_1_1921
                }

                /*1-3-14批量查询编制列表（已使用编制数）[1282]   */
                Assert.isNull(batchQueryPostMatchingStaffingReq_1.getSubjectLifeCycle(), "D1-3查询编制统计（边界）-1-3-14批量查询编制列表（已使用编制数）-主体生命周期不能为空", false);
                Assert.isNull(batchQueryPostMatchingStaffingReq_1.getInductionState(), "D1-3查询编制统计（边界）-1-3-14批量查询编制列表（已使用编制数）-编制是否在职不能为空", false);
                listStaffing_2 = mStaffingService.batchQueryPostMatchingStaffing(batchQueryPostMatchingStaffingReq_1);


            }
            List<Staffing> listStaffing_4 = new ArrayList<>();
            if (listPosition != null && listPosition.size() > 0) {
                BatchQueryPostMatchingStaffingReq batchQueryPostMatchingStaffingReq_2 = new BatchQueryPostMatchingStaffingReq();
                batchQueryPostMatchingStaffingReq_2.setStaffingList(listPosition.stream().map(item -> item.getPositionId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:52315_1_1922
                batchQueryPostMatchingStaffingReq_2.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:52316_1_1922
                batchQueryPostMatchingStaffingReq_2.setInductionState("FALSE");//sourceId:52318_1_1922
                if (reqDto != null) {
                    batchQueryPostMatchingStaffingReq_2.setOrganizationId(reqDto.getOrganizationId());//SimpleFieldAssign//sourceId:52499_1_1922
                    batchQueryPostMatchingStaffingReq_2.setDeptId(reqDto.getDeptId());//SimpleFieldAssign//sourceId:52500_1_1922
                }

                /*1-3-14批量查询编制列表（空编制数）[1282]   */

                listStaffing_4 = mStaffingService.batchQueryPostMatchingStaffing(batchQueryPostMatchingStaffingReq_2);


            }
            List<Staffing> listStaffing_6 = new ArrayList<>();
            if (listPosition != null && listPosition.size() > 0) {
                BatchQueryPostMatchingStaffingReq batchQueryPostMatchingStaffingReq_3 = new BatchQueryPostMatchingStaffingReq();
                batchQueryPostMatchingStaffingReq_3.setStaffingList(listPosition.stream().map(item -> item.getPositionId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:52323_1_1923
                batchQueryPostMatchingStaffingReq_3.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:52324_1_1923
                batchQueryPostMatchingStaffingReq_3.setTempLock("TRUE");//sourceId:52502_1_1923
                if (reqDto != null) {
                    batchQueryPostMatchingStaffingReq_3.setOrganizationId(reqDto.getOrganizationId());//SimpleFieldAssign//sourceId:52503_1_1923
                    batchQueryPostMatchingStaffingReq_3.setDeptId(reqDto.getDeptId());//SimpleFieldAssign//sourceId:52504_1_1923
                }

                /*1-3-14批量查询编制列表（锁定编制数）[1282]   */
                Assert.isNull(batchQueryPostMatchingStaffingReq_3.getSubjectLifeCycle(), "D1-3查询编制统计（边界）-1-3-14批量查询编制列表（锁定编制数）-主体生命周期不能为空", false);
                Assert.isNull(batchQueryPostMatchingStaffingReq_3.getTempLock(), "D1-3查询编制统计（边界）-1-3-14批量查询编制列表（锁定编制数）-临时锁定状态不能为空", false);
                listStaffing_6 = mStaffingService.batchQueryPostMatchingStaffing(batchQueryPostMatchingStaffingReq_3);


            }
//ModelCode: getDataCount
            QueryStaffingStatisticsListRespDto getDataCountRes = null;
            if (listStaffing != null && listStaffing.size() > 0 && listStaffing_4 != null && listStaffing_4.size() > 0 && listStaffing_2 != null && listStaffing_2.size() > 0) {
                QueryStaffingStatisticsListReqDto getDataCountReq = new QueryStaffingStatisticsListReqDto();
                getDataCountReq.setAllStaffingAmountList(listStaffing.stream().map(item -> item.getStaffingId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:52383_1_1924
                getDataCountReq.setEmptyHeadcountList(listStaffing_4.stream().map(item -> item.getStaffingId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:52388_1_1924
                getDataCountReq.setUseStaffingAmountList(listStaffing_2.stream().map(item -> item.getStaffingId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:52389_1_1924
                getDataCountReq.setLockingStaffingAmountList(listStaffing_4.stream().map(item -> item.getStaffingId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:52390_1_1924

                /*M-编制统计（特殊方法）[1735]  入参一个数据集，返回数据集条数 */

                getDataCountRes = nbStatisticalData.queryStaffingStatisticsList(getDataCountReq);


                getDataCountRes_1 = getDataCountRes;
            }
//processBranchName:正常结束 ,processBranchId:1925

        } else if ((reqDto != null && reqDto.getDeptId() != null || reqDto != null && reqDto.getOrganizationId() != null || reqDto != null && reqDto.getPositionId() != null) && (reqDto != null && reqDto.getStationId() == null && reqDto != null && reqDto.getPositionGradeId() == null)) {
            //elseif((D1-3查询编制统计（边界）.部门ID 值不等于空  or D1-3查询编制统计（边界）.组织ID 值不等于空  or D1-3查询编制统计（边界）.职务ID 值不等于空 ) and (D1-3查询编制统计（边界）.关联岗位ID 值等于空  and D1-3查询编制统计（边界）.职务级别ID 值等于空 ))  1927

            List<Staffing> listStaffing_8 = new ArrayList<>();
            QueryStaffingListReq queryStaffingListReq = new QueryStaffingListReq();
            queryStaffingListReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:52878_1_1928
            if (reqDto != null) {
                queryStaffingListReq.setOrganizationId(reqDto.getOrganizationId());//SimpleFieldAssign//sourceId:52875_1_1928
                queryStaffingListReq.setDeptId(reqDto.getDeptId());//SimpleFieldAssign//sourceId:52876_1_1928
                queryStaffingListReq.setPositionId(reqDto.getPositionId());//SimpleFieldAssign//sourceId:52877_1_1928
            }

            /*1-3-15查询编制列表（总编制数）[192]   */
            Assert.isNull(queryStaffingListReq.getSubjectLifeCycle(), "D1-3查询编制统计（边界）-1-3-15查询编制列表（总编制数）-主体生命周期不能为空", false);
            listStaffing_8 = mStaffingService.queryStaffingList(queryStaffingListReq);


            List<Staffing> listStaffing_10 = new ArrayList<>();
            QueryStaffingListReq queryStaffingListReq_1 = new QueryStaffingListReq();
            queryStaffingListReq_1.setInductionState("TRUE");//sourceId:52974_1_1929
            queryStaffingListReq_1.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:52978_1_1929
            if (reqDto != null) {
                queryStaffingListReq_1.setOrganizationId(reqDto.getOrganizationId());//SimpleFieldAssign//sourceId:52975_1_1929
                queryStaffingListReq_1.setDeptId(reqDto.getDeptId());//SimpleFieldAssign//sourceId:52976_1_1929
                queryStaffingListReq_1.setPositionId(reqDto.getPositionId());//SimpleFieldAssign//sourceId:52977_1_1929
            }

            /*1-3-15查询编制列表（已使用编制数）[192]   */
            Assert.isNull(queryStaffingListReq_1.getInductionState(), "D1-3查询编制统计（边界）-1-3-15查询编制列表（已使用编制数）-编制是否在职不能为空", false);
            Assert.isNull(queryStaffingListReq_1.getSubjectLifeCycle(), "D1-3查询编制统计（边界）-1-3-15查询编制列表（已使用编制数）-主体生命周期不能为空", false);
            listStaffing_10 = mStaffingService.queryStaffingList(queryStaffingListReq_1);


            List<Staffing> listStaffing_12 = new ArrayList<>();
            QueryStaffingListReq queryStaffingListReq_2 = new QueryStaffingListReq();
            queryStaffingListReq_2.setInductionState("FALSE");//sourceId:52979_1_1930
            queryStaffingListReq_2.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:52983_1_1930
            if (reqDto != null) {
                queryStaffingListReq_2.setOrganizationId(reqDto.getOrganizationId());//SimpleFieldAssign//sourceId:52980_1_1930
                queryStaffingListReq_2.setDeptId(reqDto.getDeptId());//SimpleFieldAssign//sourceId:52981_1_1930
                queryStaffingListReq_2.setPositionId(reqDto.getPositionId());//SimpleFieldAssign//sourceId:52982_1_1930
            }

            /*1-3-15查询编制列表（空编制数）[192]   */
            Assert.isNull(queryStaffingListReq_2.getInductionState(), "D1-3查询编制统计（边界）-1-3-15查询编制列表（空编制数）-编制是否在职不能为空", false);
            Assert.isNull(queryStaffingListReq_2.getSubjectLifeCycle(), "D1-3查询编制统计（边界）-1-3-15查询编制列表（空编制数）-主体生命周期不能为空", false);
            listStaffing_12 = mStaffingService.queryStaffingList(queryStaffingListReq_2);


            List<Staffing> listStaffing_14 = new ArrayList<>();
            QueryStaffingListReq queryStaffingListReq_3 = new QueryStaffingListReq();
            queryStaffingListReq_3.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:52989_1_1931
            queryStaffingListReq_3.setTempLock("TRUE");//sourceId:52990_1_1931
            if (reqDto != null) {
                queryStaffingListReq_3.setOrganizationId(reqDto.getOrganizationId());//SimpleFieldAssign//sourceId:52986_1_1931
                queryStaffingListReq_3.setDeptId(reqDto.getDeptId());//SimpleFieldAssign//sourceId:52987_1_1931
                queryStaffingListReq_3.setPositionId(reqDto.getPositionId());//SimpleFieldAssign//sourceId:52988_1_1931
            }

            /*1-3-15查询编制列表（锁定编制数）[192]   */
            Assert.isNull(queryStaffingListReq_3.getSubjectLifeCycle(), "D1-3查询编制统计（边界）-1-3-15查询编制列表（锁定编制数）-主体生命周期不能为空", false);
            Assert.isNull(queryStaffingListReq_3.getTempLock(), "D1-3查询编制统计（边界）-1-3-15查询编制列表（锁定编制数）-临时锁定状态不能为空", false);
            listStaffing_14 = mStaffingService.queryStaffingList(queryStaffingListReq_3);


//ModelCode: getDataCount
            QueryStaffingStatisticsListRespDto getDataCountRes_2 = null;
            if (listStaffing_8 != null && listStaffing_8.size() > 0 && listStaffing_12 != null && listStaffing_12.size() > 0 && listStaffing_10 != null && listStaffing_10.size() > 0 && listStaffing_14 != null && listStaffing_14.size() > 0) {
                QueryStaffingStatisticsListReqDto getDataCountReq_1 = new QueryStaffingStatisticsListReqDto();
                getDataCountReq_1.setAllStaffingAmountList(listStaffing_8.stream().map(item -> item.getStaffingId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:52383_1_1933
                getDataCountReq_1.setEmptyHeadcountList(listStaffing_12.stream().map(item -> item.getStaffingId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:52388_1_1933
                getDataCountReq_1.setUseStaffingAmountList(listStaffing_10.stream().map(item -> item.getStaffingId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:52389_1_1933
                getDataCountReq_1.setLockingStaffingAmountList(listStaffing_14.stream().map(item -> item.getStaffingId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:52390_1_1933

                /*M-编制统计（特殊方法）[1735]  入参一个数据集，返回数据集条数 */

                getDataCountRes_2 = nbStatisticalData.queryStaffingStatisticsList(getDataCountReq_1);


                getDataCountRes_1 = getDataCountRes_2;
            }
//processBranchName:正常结束 ,processBranchId:1932

        } else {
            //else  1986

//异常结束 1987
            throw new BizException("C00028", "对不起，没有数据！！", false);
        }
        QueryPostStaffingStatisticsListRespDto retData = new QueryPostStaffingStatisticsListRespDto();
        if (getDataCountRes_1 != null) {
            retData.setAllStaffingAmount(getDataCountRes_1.getAllStaffingAmount());//SimpleFieldAssign//sourceId:52438_1
            retData.setEmptyHeadcount(getDataCountRes_1.getEmptyHeadcount());//SimpleFieldAssign//sourceId:52439_1
            retData.setUseStaffingAmount(getDataCountRes_1.getUseStaffingAmount());//SimpleFieldAssign//sourceId:52440_1
            retData.setLockingStaffingAmount(getDataCountRes_1.getLockingStaffingAmount());//SimpleFieldAssign//sourceId:52441_1
        }


        return retData;
    }
    //
}
